$(function () {
    var projectName = $('#projectName').attr('project');
    console.log(projectName);
    var companyId = $('#saleCompany').val();

    /*购车客户输入框失去焦点事件 判断是否存在*/
    $('#saleCustomerName').blur(function () {
        var customerPhone = $(this).val();
        console.log(customerPhone);

        /*ajax获取查询的json数据*/
        $.getJSON(
            projectName + '/sale/getCustomerByPhone',
            {customerPhone:customerPhone},
            function (json) {
                // var customerList = json.data;
                // console.log(customerList);
                if (json.code==200) {
                    var customerList = json.data;
                    var customerId = customerList.customerId;
                    console.log(customerId);
                    //如果是true代表公司名称正确
                    $(this).removeAttr("style");
                    //解除按钮禁止
                    $('#saleButtonSubmit').attr('class', 'btn btn-success submit');
                    $('#saleCustomerId').val(customerId);
                } else {
                    //如果是false代表公司名称错误
                    $(this).attr('style', 'border: red 2px solid');
                    //禁止提交按钮点击
                    $('#saleButtonSubmit').attr('class', 'btn btn-danger disabled');
                }
            });
    });

    /*获取所有库存的车辆*/
    $.getJSON(
        projectName + '/sale/getAllRepertoryCarName',
        function (json) {
            var repertoryList = json.data;
            console.log(repertoryList);
            //将所有库存的汽车展现在下拉框中
            if(json.code==200){
                $.each(repertoryList,function (index,repertory) {
                    console.log(repertory);
                    var option = "<option value='" + repertory.carId  +"' seriesId='" + repertory.car.seriesId +
                        "'purchasePrice='" + repertory.purchasePrice + "'>" +
                        repertory.car.carName + "</option>";
                    $("#saleSelectBrand").append(option);
                });
            }
        });
    
    /*汽车名称变动事件*/
    $('#saleSelectBrand').change(function () {
        var seriesId = $("#saleSelectBrand option:selected").attr('seriesId');
        var purchasePrice = $("#saleSelectBrand option:selected").attr('purchasePrice');
        console.log(seriesId);
        console.log(purchasePrice);
        //清空车系和品牌
        $('#saleCarSeries').val('');
        $('#saleCarBrand').val('');
        $('#saleCurPrice').val('');

        /*ajax获取汽车车系的json数据*/
        $.getJSON(
            projectName + '/sale/getCarSeriesById',
            {seriesId:seriesId},
            function (json) {
                var carseriesById = json.data;
                console.log(carseriesById);

                $('#saleCarSeries').val(carseriesById.seriesName);
                $('#saleCarBrand').val(carseriesById.carbrand.brandName);
                $('#saleCurPrice').val(purchasePrice);

            });

    });

    /*根据公司编号查询销售员工*/
    $.getJSON(
        projectName + '/sale/getAllEmpByCompanyId',
        function (json) {
            var employeeList = json.data;
            console.log(employeeList);
            //将所有销售人员展现在下拉框中
            for (var i = 0; i < employeeList.length; i++ ) {
                var employee = employeeList[i];
                console.log(employee);
                var option = "<option value='" + employee.empid + "'>" +
                    employee.empname + "</option>";
                $("#saleEmpId").append(option);
            }
        });


    $('.btn').click(function () {
        const count = $('.btn').attr('vs');
        const totalPrice = $("[vs='" + count + "']").attr('v');
        $('#customerPayment').on('show.bs.modal', function (e) {
            $('#totalPrice').text(totalPrice);
        })
        $('#customerPayment').modal();
    });
    $('#payment_id').click(function () {
        var saleId = $('.btn').attr('saleId');
        $.getJSON(
            projectName+"/sale/updateSaleById/"+saleId,
            function (result) {
                if(result.code==200){
                    $('#customerPayment').modal('hide');
                    location.href=projectName+'/sale/getSaleList.do';
                }else {
                    alert("支付失败!");
                }
            }
        );
    });
});